package com.jiu.ui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.jiu.bean.MyJPcap;
import com.jiu.core.Account;
import com.jiu.core.FishClient;
import com.jiu.core.FishContext;
import com.jiu.util.JavaScriptUtils;


public class LoginFrame extends BaseFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7903547113406782282L;
	// 窗口的用户编码的输出框
	private JComboBox<String> idField;
	private JPasswordField pwdField;
	private JTextField vcodeField;
	// 用于提示用许信息的
	private JLabel message;
	private FishContext context;


	// 窗口的构造方法
	public LoginFrame(FishClient context) {
		this.context = context;
		init();
	}

	// 初始化窗口的方法
	public void init() {
		this.setSize(260, 200);
		this.setLocationRelativeTo(null);
		this.setContentPane(createContentPane());
	}

	// 创建主panel并初始化
	public JPanel createContentPane() {
		JPanel panel = new JPanel(new BorderLayout());
		JLabel lable = new JLabel("用户登录", JLabel.CENTER);
		panel.add(lable, BorderLayout.NORTH);
		panel.add(createCenterPane(), BorderLayout.CENTER);
		panel.add(createBtnPane(), BorderLayout.SOUTH);
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
		// 上 左 下 右
		return panel;
	}

	// 创建放在中间的panel
	public JPanel createCenterPane() {
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(createIdOrPwdPane(), BorderLayout.NORTH);
		// 创建用于给用户提示信息的JLabel
		message = new JLabel("", JLabel.CENTER);
		panel.add(message, BorderLayout.CENTER);
		return panel;
	}

	// 放置两个lable和两个输入况的panel
	public JPanel createIdOrPwdPane() {
		JPanel panel = new JPanel(new GridLayout(3, 1,0, 3));
		panel.setBorder(new EmptyBorder(8, 8, 8, 8));
		panel.add(createIdPane());// 添加存放编号输入框的panel
		panel.add(createPwdPane());// 添加存放密码输入况的panel
		panel.add(createVcodePane());// 添加存放验证码输入况的panel
		return panel;
	}

	public JPanel createVcodePane() {
		JPanel panel = new JPanel(new BorderLayout(6, 0));
		JLabel label = new JLabel("监控群：");
		panel.add(label, BorderLayout.WEST);
		// 创建一个输入框放在CENTER  
		vcodeField = new JTextField();
		panel.add(vcodeField, BorderLayout.CENTER);
		return panel;
	}

	// 创建存放编号和他的输入框的panel
	public JPanel createIdPane() {
		JPanel panel = new JPanel(new BorderLayout(6, 0));
		JLabel label = new JLabel("Ｑ　Ｑ：");
		panel.add(label, BorderLayout.WEST);
		// 创建一个输入框放在CENTER 
		idField = new JComboBox<String>();	
		idField.setEditable(true);
		panel.add(idField, BorderLayout.CENTER);
		idField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 当电机登录按钮 调用控制起ClientContext的登录方法
				context.updateLoginInfo(getUserId());
			}

		});
		return panel;
	}

	// 创建存放密码输入况的panel
	public JPanel createPwdPane() {
		JPanel panel = new JPanel(new BorderLayout(6, 0));
		JLabel label = new JLabel("密　码：");
		panel.add(label, BorderLayout.WEST);
		pwdField = new JPasswordField();
		// 密码框允许输入发（解决linux不能输入的问题）
		pwdField.enableInputMethods(true);

		panel.add(pwdField, BorderLayout.CENTER);
		return panel;
	}


	// 创建放按钮的panel
	public JPanel createBtnPane() {
		JPanel panel = new JPanel();
		JButton login = new JButton("登录");
		login.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 当电机登录按钮 调用控制起ClientContext的登录方法
				fishLogin();
			}
		});
		JButton cancel = new JButton("取消");
		cancel.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// 当点击cancel按钮后， 调用控制器ClientContext的exit方法
				exit(LoginFrame.this);
			}
		});
		
		panel.add(login);
		panel.add(cancel);
		return panel;
	}
	private void fishLogin() {
		
		Account account = context.getAccount();
		account.setQq(context.getLoginFrame().getUserId());
		account.setProxyPwd(context.getLoginFrame().getUserPwd());
		account.setNickname(context.getLoginFrame().getNickName());
		String rightMd5  = null;
		String inputMd5 = JOptionPane.showInputDialog(this,"请输入登录码！",account.getLoginCode()==null?"":account.getLoginCode());
		DateFormat fmt = new SimpleDateFormat("yyyy-MM");
		try {			
			rightMd5 = JavaScriptUtils.md5(account.getQq()+fmt.format(new Date()));
		} catch (Exception e) {}
		if(rightMd5.equalsIgnoreCase(inputMd5)){
			context.getLoginFrame().setVisible(false);
			context.getMainFrame().setTitle("钓鱼小玩应　　"+account.getNickname()+"(" + account.getQq()+")");
			context.getMainFrame().setVisible(true);
			new MyJPcap(context).start();
		}else{
			JOptionPane.showMessageDialog(this, "登录码错误或已过期，请获取新的登录码！");
		}
	}
	
	
	
	
	
	
	
	// 获取用户编码框中的内容
	public String getUserId() {
		// 获取输入框的内容
		return (String) idField.getSelectedItem();
	}
	public String getNickName() {
		// 获取输入框的内容
		return vcodeField.getText();
	}
	public void setNickName(String userName){
	    this.vcodeField.setText(userName);
	}
	// 获取用户密码框中的内容
	public String getUserPwd() {
		// return pwdField.getText();不要全
		return new String(pwdField.getPassword());
	}
	public void setUserPwd(String pwd){
	    this.pwdField.setText(pwd);
	}
	public void addJComboxItem(String key) {
		this.idField.addItem(key);
	}
	public FishContext getContext() {
		return context;
	}
	public void setContext(FishContext context) {
		this.context = context;
	}
	/* 显示用户提示信息（错误）
	public void showErrow(String info) {
		message.setText(info);// 在label中显示错误信息
		message.setForeground(Color.RED);
		final Timer timer = new Timer();
		final Point point = this.getLocation();// 返回窗口的左上角坐标
		timer.schedule(new TimerTask() {
			int i=0;
			public void run() {
				Point p = getLocation();
				 
				 int[] location = {-10,-5,0,5,10,5,0,-5}; 
				 							
				  p.x += location[i++%8];
				  setLocation(p);
				 
			}
		}, 0, 100);
		timer.schedule(new TimerTask() {
			public void run() {
				timer.cancel();
				setLocation(point);
			}
		}, 3000);
	}*/
}
